package com.wokeMy.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bean.BankAccountAndPayChannel;
import com.wokeMy.view.channe.huicao.HuicSelectActivity;
import com.wokeMy.view.channe.yibao.YbSelectActivity;
import com.zhongcai.online.R;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.woke.adapter.OnItemClickListener;
import com.woke.data.Datas_bankedcard;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.wokeMy.view.adpter.PassAdapter;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.channe.Jiupai.JiupaiActivity;
import com.wokeMy.view.channe.changjie.ChangjieRzActivity;
import com.wokeMy.view.channe.changjie.CjieKjzfActivity;
import com.wokeMy.view.channe.gaohuitong.GhtSelectActivity;
import com.wokeMy.view.channe.kalian.KlSelectActivity;
import com.wokeMy.view.channe.mobao.MobaoczActivity;
import com.wokeMy.view.channe.rongbangfd.RbSelectActivity;
import com.wokeMy.view.channe.rongbangjf.RbjfSelectActivity;
import com.wokeMy.view.channe.ronghui.RhKjzfActivity;
import com.wokeMy.view.channe.ronghui.RhuiRZhuActivity;
import com.wokeMy.view.channe.ronghui.RhuiZwmActivity;
import com.wokeMy.view.channe.tongfutong.TftZfActivity;
import com.wokeMy.view.channe.zhangcaitong.SeletCardActivity;
import com.wokeMy.view.channe.zhongfu.ZhongfuActivity;
import com.wokeMy.view.channe.huicao.LotteryHtmlActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class Mywalletnocard_Pass extends BaseActivity implements View.OnClickListener{

    ListView passLv;
    PassAdapter passAdapter;
//    int [] serviceData={0,1,2,3,4,5,6};

    TextView money_MywalletPass_tv;
    OnItemClickListener  onItemClickListener;


    private float money;
    private MyApp application;
    private ArrayList<Datas_bankedcard> mBankedCards = new ArrayList<Datas_bankedcard> ();
    private ArrayList<UnionPayChannel> mUnionPayChannels = new ArrayList<>();

    Dialog dialog;
    String controller;
    int way;
    TextView passflsm_text_title;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_mywalletnocard__pass);
//        EventBus.getDefault ().register (this);
    }

    @Override
    public void initActivity() {
        Log.e("initActivity",money+"");
        money_MywalletPass_tv.setText("￥"+money);
        Log.e("initActivity",money_MywalletPass_tv.getText()+"");
        passAdapter = new PassAdapter(getApplicationContext(),mUnionPayChannels);
        passAdapter.setOnItemClickListener(onItemClickListener);
        passLv.setAdapter(passAdapter);
        passflsm_text_title.setOnClickListener(this);
    }

    @Override
    public void initView() {
        passLv=myFindViewById(R.id.pass_select_lv);
        myFindViewById(R.id.avmywalletnocard_image_back).setOnClickListener (this);
        money_MywalletPass_tv = myFindViewById(R.id.money_MywalletPass_tv);
        passflsm_text_title = myFindViewById(R.id.passflsm_text_title);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void initData() {
        Intent intent = getIntent ();
        money = intent.getFloatExtra ("money", 0.01f);
        application = (MyApp) getApplication ();
        dialog= Util.createLoadingDialog(Mywalletnocard_Pass.this);
        myGetbanklistAndUnionPayChannel();
        getbanklistAndUnionPayChannel();
        onItemClickListener=new OnItemClickListener() {
            @Override
            public void OnItemClick(View view, final int position) {
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (!PassfastClick()){
                            //回调快速点击
                            Log.e("mUnionlsway_id",mUnionPayChannels.toString());
                            way=mUnionPayChannels.get(position).getWay();
                            controller=mUnionPayChannels.get(position).getController();
                            UnionPayChannel unionPayChannel=mUnionPayChannels.get(position);
                            if (way==71||way==72||way==73){
                                way=73;
                                //掌财通
                                if (money<unionPayChannel.getMinamount()){
                                    aralogid("交易金额小于单笔最大额度");
                                }else if (money>unionPayChannel.getMaxamount()){
                                    aralogid("交易金额大于单笔最大额度");
                                }else {
                                    Intent intent = new Intent (Mywalletnocard_Pass.this,SeletCardActivity.class);
                                    intent.putExtra ("money",money);
                                    intent.putExtra("way_id",way);
                                    startActivity(intent);
                                }

                            }else{
                                if (money<unionPayChannel.getMinamount()){
                                    aralogid("交易金额小于单笔最大额度");
                                }else if (money>unionPayChannel.getMaxamount()){
                                    aralogid("交易金额大于单笔最大额度");
                                }else {
                                    isChannelRz(controller,mUnionPayChannels.get(position));
                                }
//                                isChannelRz(controller,mUnionPayChannels.get(position));
                            }

                            Log.e("mUonPayChanstion)",""+way);
                        }
                    }
                });
            }
            @Override
            public void OnItemClick(View view, Object object, int position) {

            }
        };
    }
    /**
     * //判断通道是否入驻
     */
    private void isChannelRz(String controller,final UnionPayChannel unionPayChannel) {
        if (application.getDatas_load () != null) {

            final int userid = Integer.parseInt (application.getDatas_load ().getId ());
            //            HttpMethods.getInstance ()
            //                    .getBankAccountAndUnionPayChannel (mAccountsAndChannelsObserver, userid, 1, 3);

            AsyncHttpClient ahc = new AsyncHttpClient();
            RequestParams params = new RequestParams();
            params.put("user_id",userid);
            params.put("controller",controller);
            Log.e(" 判断通道是否入驻params",params.toString());
            Log.e(" 判断通道是否入驻地址",Constant.ISPASSRZ_URL);

            //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
            ahc.post(Constant.ISPASSRZ_URL, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    String result = null;
                    try {
                        result = new String(bytes, "utf-8");
                        //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                        Log.e("判断通道是否入驻", result);
                        try {
                            JSONObject jsonObject =new JSONObject(result);
                            final Intent intent2 = new Intent();
                            if (jsonObject.has("data")){
                                JSONObject jsonObject1 = jsonObject.getJSONObject("data").getJSONObject("data");
                                int status=jsonObject1.getInt("status");
                                switch (way) {
                                    case 75:
                                        //北京融汇
                                        Intent intent = new Intent();
                                        if (status==1){
                                            //已入住过 跳到融汇支付
                                            intent.setClass (Mywalletnocard_Pass.this,
                                                    RhKjzfActivity.class);
                                            intent.putExtra("UnionPayChannel",unionPayChannel);
                                            intent.putExtra ("money",money);
                                            startActivity (intent);
                                        }else {
                                            //未入住 跳到融汇入驻；
                                            intent.setClass (Mywalletnocard_Pass.this,
                                                    RhuiRZhuActivity.class);
                                            intent.putExtra("UnionPayChannel",unionPayChannel);
                                            intent.putExtra ("money",money);
                                            startActivity (intent);
                                        }
                                        break;
                                    case 79:
                                        //融汇 银联二维码
                                        if (status==1){
                                            //已入住过 跳到融汇银联二维码
                                            intent2.setClass (Mywalletnocard_Pass.this,
                                                    RhuiZwmActivity.class);
                                            intent2.putExtra("UnionPayChannel",unionPayChannel);
                                            intent2.putExtra ("money",money);
                                            intent2.putExtra ("flag","rhzwm");
                                            startActivity (intent2);
                                        }else {
                                            //未入住 跳到融汇入驻；
                                            intent2.setClass (Mywalletnocard_Pass.this,
                                                    RhuiRZhuActivity.class);
                                            intent2.putExtra("UnionPayChannel",unionPayChannel);
                                            intent2.putExtra ("money",money);
                                            startActivity (intent2);
                                        }
                                        break;
                                    case 76:
                                        //                                    //汇享
                                        //                                    if (status==1){
                                        //                                        //已入住过 跳到汇享支付
                                        //                                        intent2.setClass (Mywalletnocard_Pass.this,
                                        //                                                HuixiangzfActivity.class);
                                        //                                        intent2.putExtra("UnionPayChannel",unionPayChannel);
                                        //                                        intent2.putExtra ("money",money);
                                        //                                        startActivity (intent2);
                                        //                                    }else {
                                        //                                        //未入住 跳到汇享入驻；
                                        //                                        intent2.setClass (Mywalletnocard_Pass.this,
                                        //                                                HuixiangRZActivity.class);
                                        //                                        intent2.putExtra("UnionPayChannel",unionPayChannel);
                                        //                                        intent2.putExtra ("money",money);
                                        //                                        startActivity (intent2);
                                        //                                    }
                                        //                                    break;
                                    case 77:
                                        //畅捷 山高
                                        if (status==1){
                                            //已入住过 畅捷 山高支付
                                            intent2.setClass (Mywalletnocard_Pass.this,
                                                    CjieKjzfActivity.class);
                                            intent2.putExtra("UnionPayChannel",unionPayChannel);
                                            intent2.putExtra ("money",money);
                                            startActivity (intent2);
                                        }else {
                                            //未入住 跳到 畅捷 山高入驻；
                                            intent2.setClass (Mywalletnocard_Pass.this,
                                                    ChangjieRzActivity.class);
                                            intent2.putExtra("UnionPayChannel",unionPayChannel);
                                            intent2.putExtra ("money",money);
                                            startActivity (intent2);
                                        }
                                        break;
                                    case 78:
                                        //畅捷 商旅
                                        if (status==1){
                                            //已入住过 畅捷 商旅支付
                                            intent2.setClass (Mywalletnocard_Pass.this,
                                                    CjieKjzfActivity.class);
                                            intent2.putExtra("UnionPayChannel",unionPayChannel);
                                            intent2.putExtra ("money",money);
                                            startActivity (intent2);
                                        }else {
                                            //未入住 跳到 畅捷 商旅入驻；
                                            intent2.setClass (Mywalletnocard_Pass.this,
                                                    ChangjieRzActivity.class);
                                            intent2.putExtra("UnionPayChannel",unionPayChannel);
                                            intent2.putExtra ("money",money);
                                            startActivity (intent2);
                                        }
                                        break;
                                    case 85:
                                        //魔宝
                                        intent2.setClass (Mywalletnocard_Pass.this,
                                                MobaoczActivity.class);
                                        intent2.putExtra("UnionPayChannel",unionPayChannel);
                                        intent2.putExtra ("money",money);
                                        intent2.putExtra ("way_id",way);
                                        startActivity (intent2);
                                        break;
                                    case 88:
                                        //九派
                                        intent2.setClass (Mywalletnocard_Pass.this,
                                                JiupaiActivity.class);
                                        intent2.putExtra("UnionPayChannel",unionPayChannel);
                                        intent2.putExtra ("money",money);
                                        intent2.putExtra ("way_id",way);
                                        startActivity (intent2);
                                        break;
                                    case 89:
                                        //高汇通
                                        intent2.setClass (Mywalletnocard_Pass.this,
                                                GhtSelectActivity.class);
                                        intent2.putExtra("UnionPayChannel",unionPayChannel);
                                        intent2.putExtra ("money",money);
                                        intent2.putExtra ("way_id",way);
                                        startActivity (intent2);
                                        break;
                                    case 90:
                                        //卡联
                                        intent2.setClass (Mywalletnocard_Pass.this,
                                                KlSelectActivity.class);
                                        intent2.putExtra("UnionPayChannel",unionPayChannel);
                                        intent2.putExtra ("money",money);
                                        intent2.putExtra ("way_id",way);
                                        startActivity (intent2);
                                        break;
                                    case 91:
                                        //腾付通

                                        intent2.setClass (Mywalletnocard_Pass.this,
                                                TftZfActivity.class);
                                        intent2.putExtra("UnionPayChannel",unionPayChannel);
                                        intent2.putExtra ("money",money);
                                        intent2.putExtra ("way_id",way);
                                        startActivity (intent2);
                                        break;
                                    case 92:
                                        //荣邦
                                        intent2.setClass (Mywalletnocard_Pass.this,
                                                RbSelectActivity.class);
                                        intent2.putExtra("UnionPayChannel",unionPayChannel);
                                        intent2.putExtra ("money",money);
                                        intent2.putExtra ("way_id",way);
                                        startActivity (intent2);
                                        break;
                                    case 95:
                                        //荣邦
                                        intent2.setClass (Mywalletnocard_Pass.this,
                                                RbjfSelectActivity.class);
                                        intent2.putExtra("UnionPayChannel",unionPayChannel);
                                        intent2.putExtra ("money",money);
                                        intent2.putExtra ("way_id",way);
                                        startActivity (intent2);
                                        break;
                                    case 102:
                                        //汇槽 快捷充值接口
                                    OkGo.<String>post(Constant.HCCZ_URL)//
                                            .tag(this)//
                                            .cacheMode(CacheMode.NO_CACHE)//
                                            .cacheKey("no_cache")   //对于无缓存模式,该参数无效
                                            .cacheTime(5000)        //对于无缓存模式,该时间无效
                                            .params("user_id",userid)//
                                            .params("way",way)
                                            .params("amount",money)
                                            .execute(new StringCallback() {
                                                @Override
                                                public void onSuccess(Response<String> response) {
                                                    JSONObject jsonObject = null;
                                                    try {
                                                        jsonObject = new JSONObject(response.body());
                                                        if (jsonObject.getString("flag").equals("success")){
                                                            String string = jsonObject.getJSONObject("data").getString("data");
                                                            intent2.setClass(Mywalletnocard_Pass.this, LotteryHtmlActivity.class);
                                                            intent2.putExtra("html",string);
                                                            intent2.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                                            startActivity(intent2);
                                                        }else {
                                                            tishi(jsonObject.getString("info"));
                                                        }

                                                    } catch (JSONException e) {
                                                        e.printStackTrace();
                                                    }

                                                }
                                            });
                                        break;
                                    case 104:
                                        //易宝
                                        intent2.setClass (Mywalletnocard_Pass.this,
                                                YbSelectActivity.class);
                                        intent2.putExtra("UnionPayChannel",unionPayChannel);
                                        intent2.putExtra ("money",money);
                                        intent2.putExtra ("way_id",way);
                                        intent2.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                        startActivity (intent2);
                                        break;
                                    case 106:
                                        //易宝
                                        intent2.setClass (Mywalletnocard_Pass.this,
                                                HuicSelectActivity.class);
                                        intent2.putExtra("UnionPayChannel",unionPayChannel);
                                        intent2.putExtra ("money",money);
                                        intent2.putExtra ("way_id",way);
                                        intent2.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                        startActivity (intent2);
                                        break;
                                    default:
                                        Toast.makeText(Mywalletnocard_Pass.this,"该通道暂未开放，敬请期待",Toast.LENGTH_SHORT).show();
                                        break;
                                }
                            }else{
                                switch (way) {
                                    case 84:
                                        //中付
                                        intent2.setClass (Mywalletnocard_Pass.this,
                                                ZhongfuActivity.class);
                                        intent2.putExtra("UnionPayChannel",unionPayChannel);
                                        intent2.putExtra ("money",money);
                                        startActivity (intent2);
                                        break;

                                    default:
                                        break;
                                }
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }
            });
        }
    }
    /**
     * 获取银行卡列表和渠道
     */
    private void myGetbanklistAndUnionPayChannel() {
        if (application.getDatas_load () != null) {

            int userid = Integer.parseInt (application.getDatas_load ().getId ());
//            HttpMethods.getInstance ()
//                    .getBankAccountAndUnionPayChannel (mAccountsAndChannelsObserver, userid, 1, 3);

            AsyncHttpClient ahc = new AsyncHttpClient();
            RequestParams params = new RequestParams();
            params.put("userid",userid);
            params.put("type",1);
            params.put("kind",3);
            Log.e(" 获取银行卡列表和渠道params",params.toString());
            //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
            ahc.post(Constant.BANKPASS_URL, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    String result = null;
                    try {
                        result = new String(bytes, "utf-8");
                        //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                        Log.e("WO获取银行卡列表和渠道", result);
                        //                    try {
                        //                        JSONObject  jsonObject =new JSONObject(result);
                        //                        JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                        //                        String respcode=jsonObject1.getString("respcode");
                        //                        if (respcode.equals("0000")){
                        //                            Toast.makeText(HuixiangRZActivity.this,"入驻成功",Toast.LENGTH_SHORT).show();
                        //                            finish();
                        //                        }else if(respcode.equals("2015")){
                        //                            Toast.makeText(HuixiangRZActivity.this,"商户已经入驻过",Toast.LENGTH_SHORT).show();
                        //                        }else {
                        //                            Toast.makeText(HuixiangRZActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                        //                        }
                        //                    } catch (JSONException e) {
                        //                        e.printStackTrace();
                        //                    }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }
            });
        }
    }

    /**
     * 获取银行卡列表和渠道
     */
    private void getbanklistAndUnionPayChannel() {
        if (application.getDatas_load () != null) {

            int userid = Integer.parseInt (application.getDatas_load ().getId ());
            HttpMethods.getInstance ()
                    .getBankAccountAndUnionPayChannel (mAccountsAndChannelsObserver, userid, 1, 3);
        }
    }

    private Observer<BankAccountAndPayChannel> mAccountsAndChannelsObserver = new Observer<BankAccountAndPayChannel> () {
        @Override
        public void onSubscribe(Disposable d) {
//            Util.closeDialog(dialog);
        }

        @Override
        public void onNext(BankAccountAndPayChannel value) {
            if (value != null) {
                List<Datas_bankedcard> baList = value.getBanklist ();
                List<UnionPayChannel> clist = value.getGalllist ();
                if (baList != null && baList.size () > 0) {
                    mBankedCards.clear ();
                    mBankedCards.addAll (baList);
                }
//                if (mBankedCards.size () > 0) {
//                    if (showed == false) {
//                        Datas_bankedcard data = mBankedCards.get (0);
//                        mEcarno.setText (data.getCardno ());
//                    } else {
//                        Datas_bankedcard data = mBankedCards.get (mBankedCards.size () - 1);
//                        mEcarno.setText (data.getCardno ());
//                    }
//
//
//
//                }

                if (clist != null && clist.size () > 0) {
                    mUnionPayChannels.clear ();
                    mUnionPayChannels.addAll (clist);
                    if (passAdapter != null) {
                        passAdapter.notifyDataSetChanged ();
                    }
                }
                Util.closeDialog(dialog);

            }
        }

        @Override
        public void onError(Throwable e) {
            Util.closeDialog(dialog);
            Toast.makeText (Mywalletnocard_Pass.this, e.getMessage (), Toast.LENGTH_SHORT)
                    .show ();
        }

        @Override
        public void onComplete() {

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.avmywalletnocard_image_back:
//                InputMethodManager imm = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE);
                finish ();
                break;
            case R.id.passflsm_text_title:
                Log.e("passflsm_text_title","passflsm_text_title");
                Intent intent= new Intent(Mywalletnocard_Pass.this,PassFlActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult (requestCode, resultCode, data);
//        Log.e("onActivityResult","requestCode"+requestCode+"czhiortxian"+data.getStringExtra("czhiortxian"));
        Log.e("onActivityResult","requestCode"+requestCode);

        //        if (requestCode == 120 && resultCode == 97) {
//            ArrayList<String> listnew = data.getStringArrayListExtra ("nocarpay");
//            mEcarno.setText (listnew.get (2));
//        }
    }

    AlertDialog alertDialog;

    private void aralogid( String kll) {
        alertDialog = new AlertDialog.Builder(Mywalletnocard_Pass.this).
                setTitle("提示").
                setMessage(kll).
                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                }).create();
        alertDialog.show();
    }



}
